import {
	getMenu
} from "@/api/login.js"
import {
	router
} from "@/router/index.js"


// import { concatArr } from "@/utils/utils.js"
function concatArr(arr1, arr2) {
	var arr = arr1.concat(arr2);
	arr = unique1(arr); //再引用上面的任意一个去重方法
	console.log("arr", arr)
	return arr;
}

export default { // 注意：全局模块中不需要开启命名空间
	state: {
		userData: {
			name: 'admin',
			headImg: '',
		},
		menuList: [],
		menuOpen: false, //菜单缩进、展开
		menuOpenKeysList: [], //打开菜单数组
		menuSelectedKeysList: ['1'], //选中菜单数组
		menuTags: [],
		breadcrumb: [], //面包屑
	},
	mutations: {
		userChange(state, data) {
			state.userData = data
		},
		menuTagsChange(state, data) {
			let isTrue = true
			let cahcheTags = state.menuTags
			for (var i = 0; i < cahcheTags.length; i++) {
				cahcheTags[i].select = false;
				if (cahcheTags[i].title == data.title && cahcheTags[i].path == data.path) {
					cahcheTags[i].select = true;
					isTrue = false
				}
			}

			if (isTrue) {
				cahcheTags.push(data)
				state.menuTags = cahcheTags
			}
		},
		menuTagsRemove(state, data) {
			let isTrue = true
			let cahcheTags = state.menuTags
			for (var i = 0; i < cahcheTags.length; i++) {
				if (cahcheTags[i].path == data) {

					console.log("cahcheTags[i]cahcheTags[i]", cahcheTags[i])
					if (cahcheTags[i].select) {
						if (i != 0) {
							console.log("cahcheTags[(i-1)]", cahcheTags[(i - 1)])
							router.push({
								path: cahcheTags[(i - 1)].path
							})
							this.commit("menuSelectedKeysListChange",cahcheTags[(i - 1)].path)
						} else {
							if(cahcheTags.length>1){
								router.push({
									path: cahcheTags[(i + 1)].path
								})
								this.commit("menuSelectedKeysListChange",cahcheTags[(i + 1)].path)
							}else{
								router.push({
									'path': '/'
								})
								this.commit("menuSelectedKeysListChange",'/')
							}
							
						}
					}
					cahcheTags.splice(i, 1);
					break;
				}
			}
			state.menuTags = cahcheTags
		},
		menuAdd(state, data) {
			state.menuList = data
		},
		menuChange(state) {
			state.menuOpen = !state.menuOpen
		},
		menuOpenKeysListChange(state, data) {
			state.menuOpenKeysList = data;
		},
		menuSelectedKeysListChange(state, data) {
			state.menuSelectedKeysList = [data]
		},
		breadcrumbChange(state, data) {
			state.breadcrumb = data
		}
	},
	actions: {
		generateRoutes({
			commit
		}) {
			return new Promise(resolve => {
				getMenu().then((response) => {
					console.log("菜单",response)
					commit('menuAdd', response.data.data)
					resolve(response.data.data)
				})
			})
		}
	},
	getters: {
		getUser(state) {
			return state.userData
		},
		getMenuTags(state) {
			return state.menuTags
		},
		getMenuList(state) {
			return state.menuList
		},
		getMenuOpen(state) {
			return state.menuOpen
		},
		getMenuSelectedList(state) {
			return state.menuSelectedKeysList
		},
		getMenuOpenList(state) {
			return state.menuOpenKeysList
		},
		getBreadcrumb(state) {
			return state.breadcrumb
		}
	}
}
